//  ========== 
//  = 我的借款产品列表 业务JS = 
//  ==========
var page1 = 1;
var page2 = 1;
var page3 = 1;
var page4 = 1;
var limit = 8;

var loanClickFlag = 0;
var timer;
var Main = function() {
	mui.init({
		swipeBack: true //启用右滑关闭功能
	});
	this.init();
	this.proj(0, 1);
};
Main.prototype = {

	init: function() {
		mui('.mui-scroll-wrapper').scroll({
			indicators: true //是否显示滚动条
		});
		var _this = this;
		mui.each(document.querySelectorAll('.mui-slider-group .mui-scroll'), function(index, pullRefreshEl) {
			mui(pullRefreshEl).pullToRefresh({
				down: {
					callback: function() {
						var self = this;
						if(index == 0) {
							page1 = 1;
						}
						if(index == 1) {
							page2 = 1;
						}
						if(index == 2) {
							page3 = 1;
						}
						if(index == 3) {
							page4 = 1;
						}
						_this.proj(index, 1, function() {
							_this._prodBindTap()
							self.endPullDownToRefresh();
						});
					}
				},
				up: {
					callback: function() {
						var self = this;
						var page;
						if(index == 0) {
							++page1;
							page = page1;
						}
						if(index == 1) {
							++page2;
							page = page2;
						}
						if(index == 2) {
							++page3;
							page = page3;
						}
						if(index == 3) {
							++page4;
							page = page4;
						}
						_this.proj(index, page, function() {
							_this._prodBindTap();
							self.endPullUpToRefresh();
						});

					}
				}
			});
		});

		_this.eventInit()

	},
	eventInit: function() { //绑定事件
		var _this = this;
		$(".mui-control-item").each(function(index) {
			this.addEventListener('tap', function() {
				page = 1;
				_this.proj(index, page);
			});
		});
	},
	_prodBindTap: function(index, param) { //列表绑定点击事件,index:0-代偿中 1-招标中 2-还款中 3-已结清
		if(index == 0) {
			//代偿中
			$(document.querySelector('.m-imme-repayment')).on('tap', function() {
				var surplus = this.getAttribute('data-surplus');
				var id = this.getAttribute('data-id');
				mui.prompt("还款金额不能大于" + surplus, "最大还款金额", '输入还款金额', ["确定", "取消"], function(e) {
					if(e.value.trim() == '' || isNaN(e.value) || e.value > surplus) {
						mui.toast("请输入还款金额，且不大于" + surplus + '元');
					} else {
						var url = server_url + "/pay/repayCompensatoryPay.do";
						var amount = e.value;
						var param = {
							"isMobile": isMobile,
							"atoryId": id,
							"checkMoney": amount,
							"memberId": Util.getLoginUser().memberId
						}
						url = UrlHelper.customUrl(UrlHelper.urlEncode(param), url);
						url = encodeURIComponent(url);
						mui.openWindow({ "url": "huifu.html?url=" + url + "&module=loan", "id": "huifu-page" });
						//url += '&memberId='+Util.getLoginUser().memberId;
						//Util.getThirdPayLink(url);
						//var openUrl = 'openUrl.html?url='+encodeURIComponent(url)+'&title='+encodeURIComponent('立即还款');
						//mui.openWindow(openUrl);
					}
				});
			});

		} else if(index == 1) {
			//招标中
			mui("#G-proj-bid-plan").on('tap', 'li', function() {
				mui.openWindow({
					url: 'bidPlanDetail.html?bidId=' + this.getAttribute('data-id'),
					id: 'bidPlanDetail',
					extras: param
				});
			});
		} else if(index == 2) { //还款中
			//			mui("#G-proj-bid-sale").on('tap','li',function(){
			//				mui.openWindow({
			//					    url:'bidSaleDetail.html?mmplanId='+this.getAttribute('data-id'),
			//					    id:'bidSaleDetail',
			//					    extras:param
			//				     }
			//				);
			//			});

			//绑定还款计划
			$(".repayment-plan-btn").off('tap');
			$(".repayment-plan-btn").on('tap', function() {
				mui.openWindow("myLoanPaymentPlan.html?bidId=" + this.getAttribute('data-id'));
			});
			//借款合同
			$(".loan-contract-btn").on('tap', function() {

			});
			//还款授权
			$(".grant-auth-btn").on('tap', function() {
				var id = this.getAttribute('data-id');
				var url = server_url + "/pay/autoRepaymentAuthorizationPay.do?isMobile=1&planId=" + id + "&actionStatus=";
				url += '&memberId=' + Util.getLoginUser().memberId;
				url += '&rtnType=json&isMobile=1';
				$.getJSON(url, {}, function(data) {
					if(data.success && data.message) {
						mui.toast(data.message);
					}
				});
			});

			function repayMsg(content, repaySuccess) {
				var index = layer.open({
					content: content,
					btn: '确定',
					shadeClose: false,
					yes: function(i) {
						layer.close(i);
						openTab("user.html", 'user');
						if( !isH5 && window.plus) {
							plus.webview.getWebviewById('user').reload(true);
							var myLoanListWebview = plus.webview.getWebviewById("myLoanList.html");
							if(myLoanListWebview){
								plus.webview.close(myLoanListWebview);
							}
						}
					}
				});
				return index;
			}

			function repay(callback) {

				var index = layer.open({
					content: '正在处理，请等待',
					shadeClose: false,
				});
				var i = 14;
				var fn = function() {
					if(i == 14 && callback) {
						callback();
					}
					$("#layui-m-layer" + index).find(".layui-m-layercont").text('正在处理，请等待  ' + i + ' 秒');
					if(i <= 0 && loanClickFlag != 0) {
						clearInterval(timer);
						loanClickFlag = 0;
						repayMsg("第三方支付延迟，请于一小时后查看账户，如有疑问，请联系客服");
						//$("#layui-m-layer" + index).find(".layui-m-layercont").text('处理中...');
					}
					i--;
				};
				timer = setInterval(fn, 1000);
				fn();
			}
			//立即还款
			$(".imme-repayment2-btn").off('tap');
			$(".imme-repayment2-btn").on('tap', function() {
				var id = this.getAttribute('data-id');
				var peridId = this.getAttribute('data-peridId');
				var proName = this.getAttribute('data-proName');
				var intenttotal = this.getAttribute('data-intenttotal');
				var url = server_url + "/pay/repayMentByLoanerPay.do?planId=" + id + "&ids=&peridId=" + peridId + "&notMoney=" + intenttotal;
				url += '&memberId=' + Util.getLoginUser().memberId;
				url += '&rtnType=json&isMobile=1';
				var msg = proName + "第" + peridId + "期\r\n还款" + intenttotal + "元";
				mui.confirm(msg, "立即还款", ["取消", "确定"], function(e) {
					if(e.index == 1) {
						if(loanClickFlag != 0) {
							mui.toast("请勿重复提交");
							return false;
						}
						loanClickFlag = 1;
						repay(function() {
							$.getJSON(url, {}, function(data) {
								clearInterval(timer);
								layer.closeAll();
								if(data.success && data.message) {
									if(data.message == '还款成功') {
										repayMsg(data.message, true);
									} else {
										repayMsg(data.message);
									}
								} else {
									Util.openMessage(data.message);
								}
								loanClickFlag = 0;
							})
						});

					}
				});
			});

		} else if(index == 3) { //已还清
			mui("#G-proj-bid-sale").on('tap', 'li', function() {
				mui.openWindow({
					url: 'bidSaleDetail.html?mmplanId=' + this.getAttribute('data-id'),
					id: 'bidSaleDetail',
					extras: param
				});
			});
		}
	},

	//项目列表
	proj: function(index, page, _callback) {
		var _this = this;
		var param = {
			isMobile: 1,
			memberId: Util.getLoginUser().memberId,
			start: (page - 1) * limit,
			limit: 8
		};
		var url = server_url + '/user/newLoanmanagementBpCustMember.do';
		var currTab;
		if(index == 0) { //代偿中
			currTab = $('#item1ul');
			url = server_url + '/compensatory/getCompensatoryListPlBidCompensatory.do';
		} else if(index == 1) { //招标中
			currTab = $('#item2ul');
			param.planstate = 1;
		} else if(index == 2) { //还款中
			currTab = $('#item3ul');
			param.planstate = 7;
		} else if(index == 3) { //已还清
			currTab = $('#item3ul');
			param.planstate = 10;
		}
		$.getJSON(url, param, function(data) {
			var _html = '';
			if(data.success) {
				if(index == 0) { //代偿中
					for(var i = 0; i < data.result.length; i++) {
						var item = data.result[i];
						//	            		 _html += '\n <!--list beg-->     ';
						_html += '\n <li class="mui-table-view-cell">';
						_html += '\n <div class="mui-table">';
						_html += '\n 	<div class="mui-table-cell mui-col-xs-10">';
						_html += '\n 		<h4 class="mui-ellipsis m-proj-tit">' + item.bidName + '</h4>';
						_html += '\n <input class="m-recharge-btn m-imme-repayment" type="button" value="立即还款" data-surplus="' + item.surplusMoney + '" data-id="' + item.id + '"/>';
						_html += '\n 	</div>';
						_html += '\n </div>';

						_html += '\n <div class="m-proj-info">';
						_html += '\n 	<div class="m-proj-box m-proj-lt">';
						_html += '\n 		<div class="m-info-top">';
						_html += '\n 			<span class="m-big">' + item.payintentPeriod + '</span>';
						_html += '\n 		</div>';
						_html += '\n 		<div class="m-info-des">代偿期数</div>';
						_html += '\n 	</div>';
						_html += '\n 	<div class="m-proj-box m-proj-cn">';
						_html += '\n 		<div class="m-info-top instalments">';
						_html += '\n 			<span class="m-big">' + item.compensatoryMoney + '</span>';
						_html += '\n 			<span class="m-unit">元</span>';
						_html += '\n 		</div>';
						_html += '\n 		<div class="m-info-des">代偿金额</div>';
						_html += '\n 	</div>';
						_html += '\n 	<div class="m-proj-box m-proj-rt">';
						_html += '\n 		<div class="m-info-top">';
						_html += '\n 			<span class="m-big">' + item.punishMoney + '</span>';
						_html += '\n 			<span class="m-unit">元</span>';
						_html += '\n 		</div>';
						_html += '\n 		<div class="m-info-des">罚息</div>';
						_html += '\n 	</div>';
						_html += '\n </div>';
						_html += '\n <div class="m-range-bar z-act"></div>';
						_html += '\n <div class="mui-table m-proj-nottom">';
						_html += '\n <div class="mui-table-cell mui-col-xs-5">';
						_html += '\n <h4 class="mui-ellipsis m-proj-tit">';
						_html += '\n 代偿日期：' + item.compensatoryDate;
						_html += '\n </h4>';
						_html += '\n </div>';
						_html += '\n  <div class="mui-table-cell mui-col-xs-5 mui-text-right">';
						_html += '\n <span class="mui-h5 m-proj-tag">剩余金额：' + item.surplusMoney + '元</span>';
						_html += '\n </div>';
						_html += '\n </div>';

						_html += '\n </li>';
						_html += '\n <!-- list end-->';

					}

				} else if(index == 1) {
					for(var i = 0; i < data.result.length; i++) {
						var item = data.result[i];
						//	            		 _html += '\n <!--list beg-->     ';
						_html += '\n <li class="mui-table-view-cell">';
						_html += '\n <div class="mui-table">';
						_html += '\n 	<div class="mui-table-cell mui-col-xs-10">';
						_html += '\n 		<h4 class="mui-ellipsis m-proj-tit">' + item.proName + '</h4>';
						_html += '\n 	</div>';
						//_html += '\n 	<!--<div class="mui-table-cell mui-col-xs-2 mui-text-right">';
						//_html += '\n 		<span class="mui-h5 m-proj-tag tag-yew">券</span>';
						//_html += '\n 		<span class="mui-h5 m-proj-tag tag-ble">新</span>';
						//_html += '\n 	</div>-->';
						_html += '\n </div>';
						_html += '\n <div class="m-proj-info">';
						_html += '\n 	<div class="m-proj-box m-proj-lt">';
						_html += '\n 		<div class="m-info-top">';
						_html += '\n 			<span class="m-big">' + item.interestRate + '</span>';
						//_html += '\n 			<span class="m-small">+2</span>';
						_html += '\n 			<span class="m-unit">%</span>';
						_html += '\n 		</div>';
						_html += '\n 		<div class="m-info-des">年化利率</div>';
						_html += '\n 	</div>';
						_html += '\n 	<div class="m-proj-box m-proj-cn">';
						_html += '\n 		<div class="m-info-top instalments">';
						_html += '\n 			<span class="m-big">' + item.loanLife + '</span>';
						//_html += '\n 			<span class="m-big">第<b class="txt">1/3</b>期</span>';
						_html += '\n 		</div>';
						_html += '\n 		<div class="m-info-des">项目期限</div>';
						_html += '\n 	</div>';
						_html += '\n 	<div class="m-proj-box m-proj-rt">';
						_html += '\n 		<div class="m-info-top">';
						_html += '\n 			<span class="m-big">' + item.afterMoney + '</span>';
						_html += '\n 			<span class="m-unit">元</span>';
						_html += '\n 		</div>';
						_html += '\n 		<div class="m-info-des">可投金额</div>';
						_html += '\n 	</div>';
						_html += '\n </div>';
						_html += '\n <div class="m-range-bar z-act">';
						_html += '\n  	<i style="width: ' + item.progress + '%;"></i>';
						_html += '\n  	<div class="m-range-round" style="left: ' + item.progress + '%;">';
						_html += '\n  		<div class="round"></div>';
						_html += '\n  	</div>';
						_html += '\n  </div>';
						_html += '\n <div class="mui-table m-proj-nottom">';
						_html += '\n 	<div class="mui-table-cell mui-col-xs-5">';
						_html += '\n 		<h4 class="mui-ellipsis m-proj-tit">';
						_html += '\n 			投标日起:<b class="txt">' + item.bidTime + '</b><b class="txt m-pl-2 m-bl-1 m-ml-2">' + item.progress + '%</b>';
						_html += '\n 		</h4>';
						_html += '\n 	</div>';
						//						_html += '\n 	<div class="mui-table-cell mui-col-xs-5 mui-text-right m-loan-box mui-clearfix">';
						//						_html += '\n 		<!--还款-->';
						//						_html += '\n 		<input class="m-recharge-btn repayment" type="button" value="还款计划"> ';
						//						_html += '\n 		<!--借款合同-->';
						//						_html += '\n 		<input class="m-recharge-btn loan" type="button" value="借款合同">';
						//						_html += '\n 	</div>';
						_html += '\n </div>';
						_html += '\n </li>';
						_html += '\n <!-- list end-->';

					}
				} else if(index == 2) {
					for(var i = 0; i < data.result.length; i++) {
						var item = data.result[i];
						_html += '\n <!--list beg-->';
						_html += '\n <!--list beg-->     ';
						_html += '\n <li class="mui-table-view-cell">';
						//_html += '\n <!--右上角状态 beg-->';
						//_html += '\n <div class="m-proj-rt-tips">';
						//_html += '\n 	<i class="mui-icon u-iconfont">&#xe683;</i>';
						//_html += '\n </div>';
						//_html += '\n <!--右上角状态 end-->';
						_html += '\n <div class="mui-table">';
						_html += '\n 	<div class="mui-table-cell mui-col-xs-10">';
						_html += '\n 		<h4 class="mui-ellipsis m-proj-tit">' + item.proName + '</h4>';
						_html += '\n 	</div>';
						//_html += '\n 	<!--<div class="mui-table-cell mui-col-xs-2 mui-text-right">';
						//_html += '\n 		<span class="mui-h5 m-proj-tag tag-yew">券</span>';
						//_html += '\n 		<span class="mui-h5 m-proj-tag tag-ble">新</span>';
						//_html += '\n 	</div>-->';
						_html += '\n </div>';
						_html += '\n <div class="m-proj-info">';
						_html += '\n 	<div class="m-proj-box m-proj-lt">';
						_html += '\n 		<div class="m-info-top">';
						_html += '\n 			<span class="m-big">' + item.payintentPeriod + '</span>';
						_html += '\n 		</div>';
						_html += '\n 		<div class="m-info-des">还款期号</div>';
						_html += '\n 	</div>';
						_html += '\n 	<div class="m-proj-box m-proj-cn">';
						_html += '\n 		<div class="m-info-top instalments">';
						_html += '\n 			<span class="m-big">' + item.intentDate + '</span>';
						_html += '\n 		</div>';
						_html += '\n 		<div class="m-info-des">还款日期</div>';
						_html += '\n 	</div>';
						_html += '\n 	<div class="m-proj-box m-proj-rt">';
						_html += '\n 		<div class="m-info-top">';
						_html += '\n 			<span class="m-big">' + item.repaymentTotal + '</span>';
						_html += '\n 			<span class="m-unit">元</span>';
						_html += '\n 		</div>';
						_html += '\n 		<div class="m-info-des">应还金额</div>';
						_html += '\n 	</div>';
						_html += '\n </div>';

						_html += '\n <div class="mui-table m-proj-nottom-btn">';
						//_html += '\n 	<div class="mui-table-cell mui-col-xs-5">';
						//_html += '\n 		<h4 class="mui-ellipsis m-proj-tit">';
						//_html += '\n 			本期还款日期:<b class="txt">'+item.bidTime+'</b><b class="txt m-pl-2 m-bl-1 m-ml-2">'+item.progress+'%</b>';
						//_html += '\n 		</h4>';
						//_html += '\n 	</div>';
						_html += '\n 	<div class="mui-table-cell mui-col-xs-5 mui-text-right m-loan-box mui-clearfix">';
						_html += '\n 		<!--立即还款-->';
						_html += '\n 		<input class="m-recharge-btn imme-repayment2-btn" type="button" value="立即还款" data-proName="' + item.proName + '" data-intenttotal="' + item.intenttotal + '" data-peridId="' + item.payintentPeriod + '" data-id="' + item.bidId + '"/>';
						_html += '\n 		<!--还款授权-->';
						_html += '\n 		<input class="m-recharge-btn grant-auth-btn" type="button" value="还款授权" data-id="' + item.bidId + '"/> ';
						_html += '\n 		<!--还款-->';
						_html += '\n 		<input class="m-recharge-btn repayment-plan-btn" type="button" value="还款计划" data-id="' + item.bidId + '"/> ';
						_html += '\n 		<!--借款合同-->';
						_html += '\n 		<input class="m-recharge-btn loan-contract-btn" type="button" value="借款合同" data-id="' + item.bidId + '"/>';
						_html += '\n 	</div>';
						_html += '\n </div>';
						_html += '\n </li>';
						_html += '\n <!-- list end-->';

					}
				} else if(index == 3) {

					for(var i = 0; i < data.result.length; i++) {
						var item = data.result[i];
						_html += '\n <!--list beg-->';
						_html += '\n <li class="mui-table-view-cell">';
						_html += '\n <!--右上角状态 beg-->';
						_html += '\n 	<div class="m-proj-rt-tips">';
						_html += '\n 	<i class="mui-icon u-iconfont">&#xe683;</i>';
						_html += '\n </div>';
						_html += '\n <!--右上角状态 end-->';
						_html += '\n 	<div class="mui-table">';
						_html += '\n <div class="mui-table-cell mui-col-xs-10">';
						_html += '\n 	<h4 class="mui-ellipsis m-proj-tit">' + item.projectName + '</h4>';
						_html += '\n </div>';
						_html += '\n </div>';
						_html += '\n <div class="m-proj-info">';
						_html += '\n <div class="m-proj-box m-proj-lt">';
						_html += '\n <div class="m-info-top">';
						_html += '\n <span class="m-big">' + item.interestRate + '</span>';
						_html += '\n <span class="m-small">+2</span>';
						_html += '\n <span class="m-unit">%</span>';
						_html += '\n </div>';
						_html += '\n <div class="m-info-des">年化利率</div>';
						_html += '\n </div>';
						_html += '\n <div class="m-proj-box m-proj-cn">';
						_html += '\n <div class="m-info-top">';
						_html += '\n <span class="m-big">' + item.loanLife + '</span>';
						/*_html+='\n <span class="m-unit">个月</span>';*/
						_html += '\n </div>';
						_html += '\n <div class="m-info-des">剩余期限</div>';
						_html += '\n </div>';
						_html += '\n <div class="m-proj-box m-proj-rt">';
						_html += '\n <div class="m-info-top">';
						_html += '\n <span class="m-big">' + item.userMoney + '</span>';
						_html += '\n <span class="m-unit">元</span>';
						_html += '\n </div>';
						_html += '\n <div class="m-info-des">加入金额</div>';
						_html += '\n </div>';
						_html += '\n </div>';
						_html += '\n <div class="m-range-bar z-act">';
						_html += '\n <i style="width: 50%;"></i>';
						_html += '\n <div class="m-range-round" style="left: 50%;">';
						_html += '\n <div class="round"></div>';
						_html += '\n </div>';
						_html += '\n </div>';
						_html += '\n <div class="mui-table m-proj-nottom">';
						_html += '\n <div class="mui-table-cell mui-col-xs-5">';
						_html += '\n <h4 class="mui-ellipsis m-proj-tit">';
						_html += '\n 投标日期:<b class="txt">' + item.bidtime + '</b>';
						_html += '\n </h4>';
						_html += '\n </div>';
						_html += '\n <div class="mui-table-cell mui-col-xs-5 mui-text-right">';
						_html += '\n <span class="mui-h5 m-proj-tag">50%</span>';
						_html += '\n </div>';
						_html += '\n </div>';
						_html += '\n </li>';
						_html += '\n <!-- list end-->';

					}

				}

			}
			if(page == 1) {
				currTab.html('');
			}
			currTab.append(_html);
			if(_this._prodBindTap) {
				_this._prodBindTap(index, {});
			}

			if($.isFunction(_callback)) {
				_callback();
			}
		});
	}

}